//
// Created by LiuDongPeng on 2024/1/26.
//

#include "board.h"



/**
 * @brief 控制板上的led
 */

// led1
#define LED1_GPIO_PORT  Led1_GPIO_Port
#define LED1_GPIO_PIN   Led1_Pin

// led2
#define LED2_GPIO_PORT  Led2_GPIO_Port
#define LED2_GPIO_PIN   Led2_Pin

// led3
#define LED3_GPIO_PORT  Led3_GPIO_Port
#define LED3_GPIO_PIN   Led3_Pin

static void led1_set_level(bool val)
{
    HAL_GPIO_WritePin(LED1_GPIO_PORT, LED1_GPIO_PIN, val ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
static void led2_set_level(bool val)
{
    HAL_GPIO_WritePin(LED2_GPIO_PORT, LED2_GPIO_PIN, val ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
static void led3_set_level(bool val)
{
    HAL_GPIO_WritePin(LED3_GPIO_PORT, LED3_GPIO_PIN, val ? GPIO_PIN_SET : GPIO_PIN_RESET);
}



/**
 * @brief 控制板上的按键
 */

// btn1
#define BTN1_GPIO_PORT  Key1_GPIO_Port
#define BTN1_GPIO_PIN   Key1_Pin

// btn2
#define BTN2_GPIO_PORT  Key2_GPIO_Port
#define BTN2_GPIO_PIN   Key2_Pin

// btn3
#define BTN3_GPIO_PORT  Key3_GPIO_Port
#define BTN3_GPIO_PIN   Key3_Pin

// btn4
#define BTN4_GPIO_PORT  Key4_GPIO_Port
#define BTN4_GPIO_PIN   Key4_Pin

static uint8_t get_btn1_level()
{
    return HAL_GPIO_ReadPin(BTN1_GPIO_PORT, BTN1_GPIO_PIN);
}
static uint8_t get_btn2_level()
{
    return HAL_GPIO_ReadPin(BTN2_GPIO_PORT, BTN2_GPIO_PIN);
}
static uint8_t get_btn3_level()
{
    return HAL_GPIO_ReadPin(BTN3_GPIO_PORT, BTN3_GPIO_PIN);
}
static uint8_t get_btn4_level()
{
    return HAL_GPIO_ReadPin(BTN4_GPIO_PORT, BTN4_GPIO_PIN);
}

/**
 * @brief 按键扫描时钟滴答，5ms调用一次
 * @return
 */
void bsp_btn_ticks()
{
    button_ticks();
}


/**
 * @brief MT6701磁编码器
 */

#define MT6701_SPI_HANDLE  hspi1
#define MT6701_SPI_CS_PORT GPIOD
#define MT6701_SPI_CS_PIN  GPIO_PIN_2

static int encoder_spi_send_recv_callback(uint8_t* pTxData, uint8_t* pRxData, uint16_t size, uint32_t timeOut)
{
    return HAL_SPI_TransmitReceive(&MT6701_SPI_HANDLE, pTxData, pRxData, size, timeOut);
}
static void encoder_spi_cs_set_callback(bool val)
{
    HAL_GPIO_WritePin(MT6701_SPI_CS_PORT, MT6701_SPI_CS_PIN, val ? GPIO_PIN_SET : GPIO_PIN_RESET);
}




Board::Board()
{
    /* Encoder init */
    encoder_ = new MT6701(encoder_spi_send_recv_callback, encoder_spi_cs_set_callback, false);

    /* Led init */
    led1_ = new Led(led1_set_level, true);
    led2_ = new Led(led2_set_level, true);
    led3_ = new Led(led3_set_level, true);

    /* Button init */
    button_create(&btn1_, 0, get_btn1_level);
    button_create(&btn2_, 0, get_btn2_level);
    button_create(&btn3_, 0, get_btn3_level);
    button_create(&btn4_, 0, get_btn4_level);

    /* ADC自校准 */
    HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
    HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED);
    HAL_Delay(100);

    /* 开启ADC注入组转换中断 */
    HAL_ADCEx_InjectedStart_IT(&hadc1);
    HAL_ADCEx_InjectedStart(&hadc2);

    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);

    /* 关闭PWM输出 */
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);

}

/**
 * @brief
 * @return
 */
std::tuple<float, float, float> Board::get_phase_current()
{
    int adcA = 0, adcB = 0, adcC = 0;

    adcA = HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_1);
    adcB = HAL_ADCEx_InjectedGetValue(&hadc2, ADC_INJECTED_RANK_1);
//    adcC = HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_2);

    float ia = (float)((adcA - iaOffset_)) * (0.008056640625f);
    float ib = (float)((adcB - ibOffset_)) * (0.008056640625f);
//    *ic = (float)((adcC - icOffset_) - 2048) * (3.3f / 4096) / (0.005f * 20);

//    motor1.phaseCurrent.ia = low_pass_filter_work(&motor1.iaLpFilter, motor1.phaseCurrent.ia);
//    motor1.phaseCurrent.ib = low_pass_filter_work(&motor1.ibLpFilter, motor1.phaseCurrent.ib);
//    motor1.phaseCurrent.ic = low_pass_filter_work(&motor1.icLpFilter, motor1.phaseCurrent.ic);

    float ic = -(ia) - (ib);

    return std::make_tuple(ia, ib, ic);
}
